#!/usr/bin/python
# This file compares the input routing table against a base file 0.0
import sys, commands
if len(sys.argv) != 2:
    print "Usages: ",sys.argv[0], "<i/p routingtable>"
    sys.exit()

#basefile = sys.argv[1][0:sys.argv[1].rfind(".0.")]+ '.0.0'
tmp = sys.argv[1].split(".")
basefile = tmp[0]+ '.graph.rt.0.0'
print sys.argv[1], basefile
fin_base = open(basefile, 'r')
baseline = fin_base.readline()
fin_2 = open(sys.argv[1],'r')
line2 = fin_2.readline()
totalroutingentries = 0
notsame = 0
baseline = fin_base.readline()
line2 = fin_2.readline()
while baseline != '' and line2 != '':
    # now extract out all the routing entries and compare them one by one
    # Now read the first node's routing table:
    baserouting_entries = []
    routing_entries = []
    base_routing_entries_count = 0
    routing_entries_count = 0
    while baseline.find("RoutingTable") <0 and baseline != '':
        if baseline.find("[") == 0:
            baserouting_entries.append("")
            base_routing_entries_count = base_routing_entries_count + 1
        baserouting_entries[base_routing_entries_count-1] = baserouting_entries[base_routing_entries_count-1] + baseline.strip()
        baseline = fin_base.readline()
    while line2.find("RoutingTable")<0 and line2 != '':
        if line2.find("[") == 0:
            routing_entries.append("")
            routing_entries_count = routing_entries_count + 1
        routing_entries[routing_entries_count-1] = routing_entries[routing_entries_count-1] + line2.strip()
        line2 = fin_2.readline()
    # now compare the routing tables for the current node:
    totalroutingentries = base_routing_entries_count + totalroutingentries
    for i in range(0, min(len(baserouting_entries), len(routing_entries) )):
        if baserouting_entries[i] != routing_entries[i]:
            print "--->\tThese 2 lines are different\n", baserouting_entries[i] , '\n------------\n', routing_entries[i]
            notsame = notsame + 1    
    baseline = fin_base.readline()
    line2 = fin_2.readline()
    if line2 == '' or baseline == '':
        break
fin_2.close()
fin_base.close()

print 'Total lines : ', totalroutingentries, ' Lines not same: ',notsame
